<template>
  <div class="viewview">
    <div>
      <div>
        <swipper :imagelist="imagelist" :autoplay="3000"></swipper>
      </div>
      <div>
        <van-notice-bar color="#1989fa" background="#ecf9ff" left-icon="info-o">通知内容</van-notice-bar>
      </div>
    </div>

    <div class="menuclass" v-if="userList.length !== 0">
      <div class="menuclass1">
        <van-dropdown-menu>
          <van-dropdown-item title="更换用户" v-model="value1" :options="userList" ref="item">
            <van-button block type="info" @click="clicklogin">添加账户</van-button>
          </van-dropdown-item>
        </van-dropdown-menu>
      </div>
      <div class="menuclass2">
        当前用户：{{dataligin}}
      </div>
    </div>

    <div class="nousertext" v-if="userList.length == 0">
      <div class="" style="padding:2rem 0">
        您还未有用户,请登录/注册
      </div>
      <div class="" style="padding:2rem 0">
        <van-button @click="clicklogin" type="info">点击注册/登录</van-button>
      </div>
    </div>

    <div
      v-if="userType==0||userType==1||userType==2"
    >
      <h2 class="van-block__title">个人车辆</h2>

      <div class="menulistview">
        <menulist :munulist="munulist"></menulist>
      </div>

    </div>

    <div
      v-if="userType == 0 && results == false"
    >
    <div class="quitbutton" @click="sqqiye">
      申请成为企业用户
    </div>
    </div>


    <div v-if="userType == 1 || userType == 2">
      <h2 class="van-block__title">企业车辆</h2>
      <div class="menulistview">
        <menulist :munulist="list"></menulist>
      </div>
    </div>

    <!-- <div class="quitbutton" @click="quit">
      退出登录
    </div> -->
    <!-- <Tabbar></Tabbar> -->
  </div>
</template>


<script>
import Tabbar from "../../components/commons/Tabbar";
import swipper from "../../components/swipper/swipper";
import menulist from "../../components/menu/menu";

import sichechuzu from "../../assets/img/sichechuzu.png";
import cheliangchaxun from "@/assets/img/cheliangchaxun.png";
import zulinxieyi from "../../assets/img/zulinxieyi.png";
import shouyichaxun from "../../assets/img/shouyichaxun.png";
import gengduo from "../../assets/img/gengduo.png";
export default {
  data() {
    return {
      userType:null,
      userinfo: null,
      results:true,
      value1: 0,
      dataligin:null,
      userList:[],
      imagelist: [
        "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3790907151,2661667990&fm=26&gp=0.jpg",
        "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3790907151,2661667990&fm=26&gp=0.jpg",
        "https://ss3.bdstatic.com/70cFv8Sh_Q1YnxGkpoWK1HF6hhy/it/u=3790907151,2661667990&fm=26&gp=0.jpg"
      ],
      munulist: [
        {
          link: "/PersonalCar/Rentout",
          imgsrc: sichechuzu,
          text: "新增车辆"
        },
        {
          link: "/PersonalCar/Vehicleinquiry",
          imgsrc: cheliangchaxun,
          text: "查询确认"
        },
        {
          link: "/PersonalCar/LeaseagreementCompanyPage",
          imgsrc: zulinxieyi,
          text: "协议确认"
        },
        {
          link: "/PersonalCar/Incomeinquiry",
          imgsrc: shouyichaxun,
          text: "我的查询"
        }
        // {
        //   link: "",
        //   imgsrc: gengduo,
        //   text: "更多"
        // }
      ],
      list: [
        {
          link: "/enterprise/info",
          imgsrc: require("@/assets/img/qiyexinxi.png"),
          text: "企业信息"
        },
        {
          link: "/enterpriseCar/car",
          imgsrc: require("@/assets/img/qiyeyongche.png"),
          text: "企业用车"
        },
        {
          link: "/enterpriseCar/contract",
          imgsrc: require("@/assets/img/zulinhetong.png"),
          text: "租赁合同"
        },
        // {
        //   imgsrc: require("@/assets/img/yingfukuanzhangdan.png"),
        //   text: "账单管理",
        //   link: "/enterprise/bill"
        // },
        {
          imgsrc: require("@/assets/img/jiayoukaguanli.png"),
          text: "加油卡管理",
          link: "/enterpriseCar/card"
        },
        {
          imgsrc: require("@/assets/img/lishizhangdan.png"),
          text: "历史账单",
          link: "/enterpriseCar/bill"
        }
      ]
    };
  },
  watch:{
    value1:function(e,v){
      let _vm = this
      let tokendata = {
        token:this.userList[e].loginToken
      }
      _vm.userType = this.userList[e].userType
      _vm.dataligin = this.userList[e].phone
      localStorage.setItem('usertoken', this.userList[e].token)
      localStorage.setItem('userType', this.userList[e].userType)
      this.$Api
        .get("/api/user/tokenLogin", tokendata )
        .then()
        .catch();


        this.$Api
          .get("/api/sqrecord/ifShenqing", {} )
          .then(res=>{
            this.results = res.data.data.result
          })
          .catch();
        // let data = {
        //   openid:localStorage.getItem("openId"),
        // }
        // this.$Api
        //   .get("/api/user/changeSysUser", data)
        //   .then(res => {
        //     if (res.data.code == 200) {
        //       let dataa = res.data.data
        //       if (dataa.length !== 0) {
        //         _vm.userType = dataa[0].userType
        //         _vm.dataligin = dataa[0].phone
        //         localStorage.setItem('usertoken', dataa[0].token)
        //         for (let i = 0; i < dataa.length; i++) {
        //           dataa[i].value = i
        //           dataa[i].text = dataa[i].phone
        //         }
        //         _vm.userList = dataa
        //       }
        //     } else {
        //       this.$dialog.toast({
        //         mes: res.data.message,
        //         timeout: 1500
        //       });
        //     }
        //   })
        //   .catch(err => {});
    }
  },
  created() {
    let _vm = this
      // this.userList = window.localStorage.getItem("userList");
      // if (this.userList) {
      //   if ( this.userList.length !== 0) {
      //     localStorage.setItem('usertoken',this.userList[0].token)
      //     this.usertype = this.userList[0].userType
      //     for (let i = 0; i < this.userList.length; i++) {
      //       this.userList[i].value = i
      //     }
      //   }
      // }
      let data = {
        openid:localStorage.getItem("openId"),
      }
      this.$Api
        .get("/api/user/changeSysUser", data)
        .then(res => {
          if (res.data.code == 200) {
            let dataa = res.data.data
            if (dataa.length !== 0) {
              _vm.userType = dataa[0].userType
              _vm.dataligin = dataa[0].phone
              for (let i = 0; i < dataa.length; i++) {
                dataa[i].value = i
                dataa[i].text = dataa[i].phone
              }
              _vm.userList = dataa
            }
          } else {
            _vm.userList = []
            // this.$dialog.toast({
            //   mes: res.data.message,
            //   timeout: 1500
            // });
          }
        })
        .catch(err => {});
  },
  components: {
    Tabbar,
    swipper,
    menulist
  },
  methods: {
    clicklogin(){
      this.$router.push({ path: "/login" });
    },
    onConfirm() {
     this.$refs.item.toggle();
   },
   sqqiye(){
     this.$Api
       .get("/api/sqrecord/sqqyUser", {})
       .then(res =>{
        if (res.data.code == 200) {
          this.$dialog.toast({
            mes: res.data.data,
            timeout: 1500
          });
          this.results = !this.results
        }else{
          this.$dialog.toast({
            mes: res.data.message,
            timeout: 1500
          });
        }
       })

   }
  }
};
</script>


<style long="scss" scoped>
.viewview {
}

.van-block__title {
  margin: 0;
  padding: 0.2rem 0.1rem 0.1rem 0.5rem;
  color: #09f;
  font-size: 1rem;
  line-height: 2.5rem;
  border-left: 0.3rem solid #09f;
  background-color: #f5f5f5;
}

.quitbutton{
  background-color: #09f;
  padding: 10px 0;
  margin: 20px;
  color: #ffffff;
  text-align: center;
}

.menuclass{
  display: flex;
  align-items: center;
  text-align:  center;
}

.nousertext{
  text-align: center;
}

.menuclass1{
  flex:1;
}

.menuclass2{
  flex:1
}
.menulistview {
  /* margin: 1rem 0; */
}
</style>
